package logic

import (
	"context"
	"errors"
	"fmt"
	"shoppingcart-srv/model/mysql"

	"shoppingcart-srv/internal/svc"
	"shoppingcart-srv/shoppingcart"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCartListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartListLogic {
	return &CartListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CartListLogic) CartList(in *shoppingcart.CartListRequest) (*shoppingcart.CartListResponse, error) {
	// todo: add your logic here and delete this line
	m := mysql.Cart{UserId: in.UserId}
	list, err := m.CartList(in.GoodsTitle) //调头model层 / CartList 购物车列表
	if err != nil {
		return nil, errors.New("查询用户列表失败")
	}
	//c := mysql.Cart{GoodsTitle: in.GoodsTitle}
	//fmt.Println(in.GoodsTitle, "--------------------------------")
	//list, err := c.List(in.GoodsTitle)
	//if err != nil {
	//	return nil, err
	//}
	fmt.Println(list, "111111111111111111111111111111111111111111")
	var CartList []*shoppingcart.CartInfo
	for _, v := range list {
		CartList = append(CartList, &shoppingcart.CartInfo{
			Id:         int64(v.ID),
			UserId:     v.UserId,
			GoodsId:    v.GoodsId,
			GoodsPrice: v.GoodsPrice,
			Quantity:   v.Quantity,
			GoodsTitle: v.GoodsTitle,
			GoodsCover: v.GoodsCover,
			Checked:    v.Checked,
		})
	}
	fmt.Println(CartList, "2222222222222222222222222222222222222")
	return &shoppingcart.CartListResponse{CartList: CartList}, nil
}
